﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Animation;
using FarseerPhysics;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Dynamics.Contacts;
using FarseerPhysics.Dynamics.Joints;
using Enemies;
using FarseerPhysics.Factories;
using Microsoft.Xna.Framework;
using AI;

namespace MapLibrary.Players.Skills
{
    class NetSprite : Sprite
    {
        const int LIFE = 500;

        private List<WeldJoint> joints;
        private List<Sprite> objectsInNet;

        public int TimeToLive { get; set; }

        public NetSprite()
        {
            MAX_VELOCITY_X = ConvertUnits.ToSimUnits(1000);
            MAX_VELOCITY_Y = ConvertUnits.ToSimUnits(1000);
            this.Controller.MaxLinearVelocityX = MAX_VELOCITY_X;
            this.Controller.MaxLinearVelocityY = MAX_VELOCITY_Y;

            joints = new List<WeldJoint>();
            objectsInNet = new List<Sprite>();

            TimeToLive = 500;
        }

        public override void Update()
        {
            base.Update();

            TimeToLive--;
            if (TimeToLive < 0)
            {
                foreach (Sprite s in objectsInNet)
                {
                    Malaria m = s as Malaria;
                    if (m != null)
                    {
                        m.Net = null;
                    }
                }

                objectsInNet.Clear();

                World.CurrentLevel.AddBodyToRemove(this);
            }
        }

        public override bool MyOnCollision(Fixture f1, Fixture f2, Contact contact)
        {
            Sprite s1 = f1.Body.UserData as Sprite;
            Sprite s2 = f2.Body.UserData as Sprite;

            if (s2 == null)
            {
                return true;
            }

            if (s2 as Player != null)
            {
                return false;
            }

            Malaria m = s2 as Malaria;
            if (m != null)
            {
                World w = World.PhysicsWorld;

                if (m.Net != null)
                {
                    return false;
                }
                if (!objectsInNet.Contains(s2))
                {
                    Vector2 d = new Vector2(MathHelper.Lerp(-0.25f, 0.25f, (float)GameWorld.Random.NextDouble()), MathHelper.Lerp(-0.25f, 0.25f, (float)GameWorld.Random.NextDouble()));
 
                    SliderJoint joint = JointFactory.CreateSliderJoint(w, s1.Body, s2.Body, Vector2.Zero, d, 0, 0.05f);
                    joint.DampingRatio = 0.6f;
                    joint.Broke += OnJointBreak;

                    m.Net = this;

                    objectsInNet.Add(s2);
                    return false;
                }
            }

            return false;
        }

        public void RemoveSprite(Sprite s)
        {
            objectsInNet.Remove(s);
        }

        public void OnJointBreak(Joint j, float f)
        {
        }
    }
}
